/*
 * Copyright (c) 2017,重庆阿莫比科技有限公司,All Rights Reserved.
 */
package com.wowocai.btchina.datasource;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;

import javax.sql.DataSource;

// 简单的实现自定义 连接池
public class SimpleDataSource implements DataSource {

    private String driver, url, username, password;

    // 通过 linkList充当 池
    private LinkedList<Connection> pool = new LinkedList<Connection>();

    // 构造 函数 初始化 连接 数目
    public SimpleDataSource(String driver, String url, String username, String password) {
        System.out.println("构造函数 创建 20个连接");
        for (int i = 0; i < 20; i++) {
            // 创建 连接
            Connection connection = JdbcUtils.getConnection(driver, url, username, password);
            // 把 创建的连接 放入池子中
            pool.add(connection);
        }
    }

    // 用完 之后 将传递的连接放回 池中
    public void addBackToPool(Connection connection) {
        pool.add(connection);
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        // TODO Auto-generated method stub

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        // TODO Auto-generated method stub
        return false;
    }

    // 从线程池 中 取得 第一个
    @Override
    public Connection getConnection() throws SQLException {
        // TODO Auto-generated method stub
        // 首先 判断 是否为空
        if (pool.isEmpty()) {
            // 为空的话 继续创建 5个连接
            for (int i = 0; i < 5; i++) {
                Connection connection = JdbcUtils.getConnection(this.getDriver(), this.getUrl(), this.getUsername(),
                        this.getPassword());
                pool.add(connection);
            }
        }
        // 有连接的话 就取出第一个
        Connection con = pool.removeFirst();
        System.out.println("取得一个连接 使用");
        return con;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        // 首先 判断 是否为空
        if (pool.isEmpty()) {
            // 为空的话 继续创建 5个连接
            for (int i = 0; i < 5; i++) {
                Connection connection = JdbcUtils.getConnection(this.getDriver(), this.getUrl(), username, password);
                pool.add(connection);
            }
        }
        // 有连接的话 就取出第一个
        Connection con = pool.removeFirst();
        System.out.println("取得一个连接 使用");
        return con;
    }

    /**
     * TODO The function of this method is described here - Optional.
     * 
     * @see javax.sql.CommonDataSource#getParentLogger()
     */
    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {

        // TODO Auto-generated method stub
        return null;
    }

    /**
     * driver.
     * 
     * @return the driver
     */

    public String getDriver() {
        return driver;
    }

    /**
     * driver.
     * 
     * @param driver the driver to set
     */

    public void setDriver(String driver) {
        this.driver = driver;
    }

    /**
     * url.
     * 
     * @return the url
     */

    public String getUrl() {
        return url;
    }

    /**
     * url.
     * 
     * @param url the url to set
     */

    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * username.
     * 
     * @return the username
     */

    public String getUsername() {
        return username;
    }

    /**
     * username.
     * 
     * @param username the username to set
     */

    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * password.
     * 
     * @return the password
     */

    public String getPassword() {
        return password;
    }

    /**
     * password.
     * 
     * @param password the password to set
     */

    public void setPassword(String password) {
        this.password = password;
    }

}
